# $Id: FormStatsParFiche.pm,v 1.4 2004/06/20 08:22:24 ibo Exp $

package CGI::FOGE2::FormStatsParFiche;
use strict;
use CGI::FOGE2::Form;
use CGI::FOGE2::Tools;

our @ISA = ();

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $params = [ @_ ];

    my $self = {
        form => undef,
    };

    bless $self, $class;
    return $self;
}

sub setform {
    my $self = shift;
    my ($form) = @_;
    $self->{form} = $form;
}

# Just report the number of total answers in HTML.

sub getstats {
    my $self = shift;
    my $n = get_answers_number($self);
    my $form = $self->{form};

    my $out = '';
    $out .= CGI::p({ -class => 'formhead'}, sprintf msg->formstats_answers, $n);
    $out .= CGI::p({ -class => 'formhead'}, "IVAN NIKOLAEV BOGOUCHEV");
    
#    my @columns = 'answer.id, ';
#
#    for my $section (@{$form->{sections}}) {
#       for my $question (@{$section->{questions}}) {
#	    push @columns , ${$question->generate_field_names()}[0];
#	}
#    }
#

#    my $columns = $self->get_all_answers();
#    $out .= CGI::table({ -border => undef, -align => 'center'},
#		       CGI::Tr( { -class => 'statrow'}, CGI::th($columns)));
    
    $out .= CGI::table({ -align => 'center'}, $self->get_all_answers());

   return $out;
}

# Compute the total number of answers.

sub get_answers_number {
    my $self = shift;
    my $form = $self->{form};
    
    # Even if we just select the DB below, we'll write soon in it:
    my $db = $form->dbh_w(); 
    die msg->die_nodbhandle unless defined $db; # extraneous

    my $answerTable = $form->{dbAnswer};
    my $request = "select count(*) 
from $answerTable where enquiry = ?;";
    my $sth = $db->prepare($request)
        || die "Cannot prepare count request: " . $db->errstr;
    $sth->execute($form->{enquiry})
        || die "Cannot execute count request: " . $db->errstr . "\n";
    my @row = $sth->fetchrow_array();
    my $n = 0;
    if ( @row ) {
        ($n) = @row;
    };
    $sth->finish();
    return $n;
}


sub get_all_answers {
    my $self = shift;
    my $form = $self->{form};
    my $db = $form->dbh_r();
    my $answerTable = $form->{dbAnswer};
    my $columns_in_tables = $self->get_tables();
    my @tables = (keys %$columns_in_tables);
    my @columns = ();

    for my $table (keys %{$columns_in_tables}) {
	for my $col (@{$$columns_in_tables{$table}}) {
	    push @columns, $col;
	}
    }
    
    return CGI::p( {-class => 'formhead'}, 'Aucune table selectionne')
	unless @tables;
    return CGI::p( {-class => 'formhead'}, 'Aucune colonne selectionne')
	unless @columns;

    my $select = join ", ", ($answerTable . '.id', @columns);
    my $from = join(", ", ($answerTable, @tables));

    my $where = $answerTable . ".id = " . $tables[0] . ".id";
    for (my $i = 0; $i < $#tables ; $i++) {
	$where .= " AND " . $tables[$i] . ".id = " . $tables[$i+1] . ".id";
    }

    print STDERR "SELECTOTOOTOOTOT e   ", $select, "\n";
    print STDERR "FROMETOOOOOO     e   ", $from, "\n";
    print STDERR "WHERETOOOOOO     e   ", $where, "\n";

    my $request = "select " . $select . "\n" .
	"from " . $from . "\n" .
	"where enquiry = \'" . $form->{enquiry} . "\' AND " . $where . ";\n";
    print STDERR $request;

    my $sth = $db->prepare($request)
        || die "Cannot prepare count request: " . $db->errstr;
    $sth->execute()
        || die "Cannot execute count request: " . $db->errstr . "\n";
 
    my $out = CGI::Tr ({ -class => 'statrow'}, CGI::th({ -class => 'statrow'}, ['', 'id', @columns]));

    while (my $row = $sth->fetchrow_arrayref()) {
	print STDERR join " | ", @$row, "\n";
	$out .= CGI::Tr( { -class => 'statrow'},
			 CGI::td([CGI::checkbox( -name => 'show_id',
						 -checked => 1,
						 -value => $$row[0],
						 -label => ''),
				  @$row]));
    }
    $sth->finish();

    return $out;    
}
 

# A hash where the keys are the table names and the
# values are arrays of the columns in those tables
sub get_tables {
    my $self = shift;
    my $form = $self->{form};
    my %tables = ();
    
    for my $section (@{$form->{sections}}) {
	my $table = $section->get_table_name();
	print STDERR "UNE TABLE:   $table .\n"; 
	#next unless defined CGI::param($table);
	for my $question (@{$section->{questions}}) {
	    print STDERR "UNE QUESTION:  ${$question->generate_field_names()}[0].\n"; 
	    #next unless defined CGI::param(${$question->generate_field_names()}[0]);
	    push @{$tables{$table}}, ${$question->generate_field_names()}[0];
	}
    }

    return \%tables;
}
1;

# end of FormStats.pm
